## Figure B12: Mobility

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)
library(tidyverse)
library(janitor)
library(lubridate)

main_data <- read.csv('./data/data_to_merge_with_mobility.csv')

## Sub-Figure A

subfigureA_data <- read.csv('./data/mobility_median_network.csv')

subfigureA_data <- subfigureA_data %>%
  pivot_longer(!COD_PROV, names_to = "date", values_to = "mobility") %>%
  mutate(start_date = as.Date(paste0("2020.",substr(date, 2, 6)), format = "%Y.%d.%m"),
         end_date = as.Date(paste0("2020.",substr(date, 8, 12)), format = "%Y.%d.%m"),
         pre.post_covid = ifelse(end_date <= as.Date("2020.01.31", format = "%Y.%m.%d"), "pre", "post"),
         week = paste0(substr(date, 5, 6),"-",substr(date, 2, 3), "~", substr(date, 11, 12), "-",  substr(date, 8, 9))) %>%
  select(-date) %>%
  filter(end_date <= as.Date('2020-04-03', format = "%Y-%m-%d"))

subfigureA_fig <- subfigureA_data %>% group_by(COD_PROV, pre.post_covid) %>% 
  summarise_at(.vars = 'mobility', .funs = mean) %>%
  right_join(main_data %>% dplyr::select(COD_PROV, excess_deaths_jan_d), by = "COD_PROV") %>%
  mutate(excess_deaths_jan_d = factor(excess_deaths_jan_d, levels = c(0,1),
                                      labels = c('low',"high")),
         type = factor(paste(pre.post_covid,excess_deaths_jan_d), 
                       levels = c('pre high', 'pre low', 'post high', 'post low'),
                       labels = c('High excess death\n\nPre-Covid',
                                  'Low excess death\n\nPre-Covid',
                                  'High excess death\n\nPost-Covid',
                                  'Low excess death\n\nPost-Covid'))) %>%
  ggplot(aes(x = type, y = mobility, fill = type, color = type)) +
  geom_boxplot(alpha=0.8, show.legend = F) +
  scale_color_manual(values=c("firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4")) +
  scale_fill_manual(values=c("firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4")) +
  xlab("") +
  ylab("Radius of Gyration") +
  stat_summary(fun=mean, geom="point", shape=20, size=5, color="white",
               position = position_dodge2(width = 0.75, preserve = "single"),show.legend = F) +
  scale_y_continuous(breaks = round(seq(1,17, by = 2),2)) +
  theme_bw() +
  theme(title = element_text(size = 13))

## Sub-Figure B

subfigureB_data <- read.csv('./data/mobility_average_network.csv')

subfigureB_data <- subfigureB_data %>%
  pivot_longer(!COD_PROV, names_to = "date", values_to = "mobility") %>%
  mutate(date = as.Date(substr(date, 2, nchar(date)), format = "%Y.%m.%d"),
         week = week(date),
         pre.post_covid = ifelse(date <= as.Date("2020.01.31", format = "%Y.%m.%d"), "pre", "post"))


subfigureB_fig <- subfigureB_data %>% group_by(COD_PROV, pre.post_covid) %>% 
  summarise_at(.vars = 'mobility', .funs = mean) %>%
  right_join(main_data %>% dplyr::select(COD_PROV, excess_deaths_jan_d), by = "COD_PROV") %>%
  mutate(excess_deaths_jan_d = factor(excess_deaths_jan_d, levels = c(0,1),
                                      labels = c('low',"high")),
         type = factor(paste(pre.post_covid,excess_deaths_jan_d), 
                       levels = c('pre high', 'pre low', 'post high', 'post low'),
                       labels = c('High excess death\n\nPre-Covid',
                                  'Low excess death\n\nPre-Covid',
                                  'High excess death\n\nPost-Covid',
                                  'Low excess death\n\nPost-Covid'))) %>%
  ggplot(aes(x = type, y = mobility, fill = type, color = type)) +
  geom_boxplot(alpha=0.8, show.legend = F) +
  scale_color_manual(values=c("firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4")) +
  scale_fill_manual(values=c("firebrick4", "dodgerblue4", "firebrick4", "dodgerblue4")) +
  xlab("") +
  ylab("Average Network Degree") +
  stat_summary(fun=mean, geom="point", shape=20, size=5, color="white",
               position = position_dodge2(width = 0.75, preserve = "single"),show.legend = F) +
  #scale_y_continuous(breaks = round(seq(1,17, by = 2),2)) +
  theme_bw() +
  theme(title = element_text(size = 13))


ggsave(filename = "./output/figureB12_a.jpeg",
       plot = subfigureA_fig,
       bg = "transparent",
       width = 10, height = 6, units = "in",dpi = 800)
ggsave(filename = "./output/figureB12_b.jpeg",
       plot = subfigureB_fig,
       bg = "transparent",
       width = 10, height = 6, units = "in",dpi = 800)
